using System;
using System.Collections.Generic;
using System.Text;
using PicoInterface;

namespace RapidHDL
{
    public static class RawIOTest
    {
        public static string Test(int piReps)
        {
            string sResult = "";
            int iLoops;

            sResult += PicoIO.Attach("cfbase.bit");
            sResult += "\n";            
            PicoInvoke.WriteDevice(PicoIO.BASE_ADDR, 5);
            if (PicoInvoke.ReadDevice(PicoIO.BASE_ADDR) != 5)
            {
                sResult += "Error On First Try";
                return sResult + "\n";
            }

            for (int iRep = 0; iRep < piReps; iRep++)
            {
                for (int iTest = 0; iTest < 10; iTest++)
                {
                    PicoInvoke.WriteDevice(PicoIO.BASE_ADDR, iTest);
                    if (PicoInvoke.ReadDevice(PicoIO.BASE_ADDR) != iTest)
                    {
                        int iResult = PicoInvoke.ReadDevice(PicoIO.BASE_ADDR);
                        sResult += "--" + PicoInvoke.GetLastError() + "--";
                        iLoops = iRep * 10 + iTest;
                        sResult += "Error On Loop " + iLoops.ToString() + " and rep " + iRep.ToString();
                        return sResult + "\n";
                    }
                }
            }

            iLoops = piReps * 10 + 10;
            sResult += "Passed " + iLoops.ToString() + " loops";
            return sResult + "\n";
        }
    }
}
